#include "com_usart1.h"

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
	
// 串口5 PC12 TX  PD2 RX
void Sys_SendLog(const char *fmt, ...) {
	uint16_t i = 0,j = 0;
    uint8_t tx_buf[500] = {0};
	
    va_list ap;
	va_start(ap,fmt);
	vsprintf((char*)tx_buf,fmt,ap);
	va_end(ap);
	i = strlen((const char*)tx_buf);
	  
    for(j = 0;j < i;j++)
	{
		while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET){};
        USART_SendData(UART5,tx_buf[j]);        
	}
	while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET);
}

void Sys_Uart5SendStr(char *ch){  
    uint16_t i = 0,j = 0;
    j = strlen(ch);
    
	for(i = 0;i < j;i++)
    {
		while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET){};
        USART_SendData(UART5,ch[i]);
    }
	while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET);        
}

// 发送数据
void Sys_Uart5SendData(uint8_t *ch,uint16_t len){  
    uint16_t i;
    for(i = 0;i < len;i++)
    {
		while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET){};
        USART_SendData(UART5,ch[i]);
    }
	while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET);        
}

void Sys_Uart5Init(uint32_t bound){
    GPIO_InitTypeDef  GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef  NVIC_InitStructure;
    //使能UART5,PD2 RX-5, PC12 TX-5
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
	
    //Uart5_TX   GPIOC12
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    //Uart5_RX GPIOD2初始化
    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    //Uart5 NVIC 配置
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    NVIC_InitStructure.NVIC_IRQChannel 					= UART5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority 		= 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd			    = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
  
   //USART 初始化设置
    USART_InitStructure.USART_BaudRate   = bound;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits   = USART_StopBits_1; 
    USART_InitStructure.USART_Parity     = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(UART5,&USART_InitStructure);
	
	USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);
    USART_Cmd(UART5, ENABLE);
}

// 判断串口01接收完成
void Sys_Uart5RecComplete(uint64_t Std_1ms){
    static uint16_t oldcount = 0;
    static uint64_t oldtime  = 0;
    
    if(oldtime > System_1ms)
        oldtime = System_1ms;  
    if(Uart5ReadFlag)
    {
		// 大于10ms
        if( (System_1ms-oldtime)>10 )
        {
            if(oldcount==Uart5ReadFlag)
            {
                Uart5ReadFlag |= (1 << 15);
                oldcount = 0;
            }
            else 
            {
                oldcount = Uart5ReadFlag;
                oldtime  = System_1ms;
            }
        }
    }	
}

// 读取USARTx->SR能避免莫名其妙的错误  	
uint8_t  Uart5ReadBuf[100] = {0};
uint16_t Uart5ReadFlag = 0;
void UART5_IRQHandler(void){
	uint8_t res = 0;
    if(USART_GetITStatus(USART3,USART_IT_RXNE) != RESET) 
	{
		res =USART_ReceiveData(UART5);

		// 是否存在数据没有处理
		if( (Uart5ReadFlag&0x8000)==0 )
		{
			Uart5ReadBuf[Uart5ReadFlag] = res;
			Uart5ReadFlag++;

			// 判断是否接收结束
			if(Uart5ReadFlag>=90)
			{
				Uart5ReadFlag |= (1 << 15);
			}
		}           
	}
	//溢出-如果发生溢出需要先读SR,再读DR,则可清除不断入中断的问题
    if( USART_GetFlagStatus(UART5,USART_FLAG_ORE)==SET )
    {
        USART_ReceiveData(UART5);
        USART_ClearFlag(UART5,USART_FLAG_ORE);
    }
	USART_ClearFlag(UART5,USART_IT_RXNE);
} 

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(UART5, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET)
  {}

  return ch;
}

